# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 13_robust_compliance.R
### This scripts tests for robustness when only including first-session attendees.
# ----
# Attended first session ----
# Exclude everyone who did not attend session 1
compliance_first_session <- bimli_svy_dkr.rm %>%
  filter(session1 == 1) %>%
  {lapply(dv_labs$dv[is.na(dv_labs$subidx)], \(x) tab_regression(x, .))} %>% # filtered out variables already included in a subindex
  bind_rows %>%
  mutate(margin = qnorm(0.975) * std.error,
         lower = estimate - margin,
         upper = estimate + margin) %>%
  filter(term != "(Intercept)" & !grepl("library_spillover_pre", term)) %>%
  select(-term)
compliance_first_session <- left_join(dv_labs, compliance_first_session, by = "dv")

# plot
compliance_first_session %>%
  filter(is.na(subidx) & secondary_outcome == FALSE & mechanism == FALSE) %>% # omit questions that are used in a subindex
  mutate(label = if_else(str_detect(label, "Index"), "Index", label),
         label = as_factor(label),
         idx = as_factor(idx),
         is.idx = if_else(label == "Index", TRUE, FALSE),
         significance = ifelse(p.value < 0.05, "YES", "NO")) %>%
  ggplot(aes(x = estimate, y = fct_rev(label), color = is.idx, linetype = significance)) +
  geom_vline(xintercept = 0, lty = 2) +
  geom_point() +
  geom_errorbar(aes(xmin = lower, xmax = upper), width = 0) +
  facet_grid(rows = "idx", scales = "free_y", space = "free_y", switch = "y") +
  xlab("Estimated effect of treatment in control-group SDs (Positive expected)") +
  scale_y_discrete(position = "right") +
  scale_color_manual(values = c("black", "red")) +
  scale_linetype_manual(values=c("dotted", "solid")) +
  theme_bw() %+replace%
  theme(axis.title.y = element_blank(),
        strip.text.y.left = element_text(angle = 0),
        legend.position = "none")

# table
compliance_first_session %>%
  filter(str_detect(label, "Index") & secondary_outcome == FALSE) %>% # only main indices
  arrange(label) %>%
  select(label, n, estimate, std.error, p.value) %>%
  mutate(label = str_remove_all(label, " Index"),
         n = format(n, big.mark = ","),
         sig.stars = case_when(p.value < 0.001 ~ "***",
                               p.value < 0.01 ~ "**",
                               p.value < 0.05 ~ "*",
                               TRUE ~ ""),
         estimate = str_c(format(round(estimate, 2), nsmall = 2), sig.stars), # add significance stars to estimate
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2)))) %>%
  select(-sig.stars) %>%
  rename(Outcome = label,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p-value" = p.value) %>%
  xtable::xtable(caption = "ITT on Session 1 Attendees",
                 align = "llllll", digits = 3,
                 label = "tab:compliance_session1") %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(pos = list(nrow(.)), 
                          command = paste(
                            "\\hline\n\\multicolumn{5}{l}{\\footnotesize *p$<$0.05; **p$<$0.01; ***p$<$0.001. Models include library-spillover strata FEs.} \\\\\n")),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = "output/tables/tab_compliance_session_1.tex")

# END of 13_robust_compliance.R ----